package com.douglas.managesystem.common;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description TODO ip工具类
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-10-26 18:08
 **/
public class IPUtil {
    public static String getIp(HttpServletRequest request){

//代理进来，则透过防火墙获取真实IP地址

        String ip = request.getHeader("X-Forwarded-For");



        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("Proxy-Client-IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("WL-Proxy-Client-IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("HTTP_CLIENT_IP");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("HTTP_X_FORWARDED_FOR");

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("X-Real-IP");

        }



//如果没有代理，则获取真实ip

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getRemoteAddr();

        }



        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;

    }
}
